<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class model_news extends CI_Model{
    var $table          = "tbl_news";
    var $table_gallery  = "tbl_gallery";
    var $table_link     = "tbl_gallery_link";
    var $state          = "master_state";
    var $primary        = "id_news";
    
    public function __construct(){
        parent::__construct();
    }
    
    function get_all_news(){
        $sql    = 'SELECT SQL_CALC_FOUND_ROWS * FROM ' . $this->table . ' ORDER BY datecreated DESC';
        $query  = $this->db->query($sql);
        return $query->result();
    }
    function get_all_news_data($limit=0, $offset=0, $conditions=''){
        if( !empty($conditions) ){
            $conditions = str_replace("%judul%",        "judul", $conditions);
            $conditions = str_replace("%content%",      "content", $conditions);
            $conditions = str_replace("%datecreated%",  "datecreated", $conditions);
        }
        
        $sql = '
            SELECT SQL_CALC_FOUND_ROWS * FROM ' . $this->table . ' ';

        if( !empty($conditions) ){
            $sql .= 'WHERE ' . $conditions . ' ';
        }
        $sql .= 'ORDER BY datecreated DESC';
        
        if( $limit ) $sql .= ' LIMIT ' . $offset . ', ' . $limit;
        
        $query = $this->db->query($sql);
        if(!$query || !$query->num_rows()) return false;
        
        return $query->result();
    }
    
    /**
     * Get news data by news ID
     * 
     * @author  Iqbal
     * @param   Integer $news_id  (Required)  News ID
     * @return  Mixed   False on failed process, otherwise object of news.
     */
    function get_newsdata($news_id){
        if ( !is_numeric($news_id) ) return false;

        $news_id = absint($news_id);
        if ( !$news_id ) return false;
        
        $query = $this->db->get_where($this->table, array($this->primary => $news_id));
        if ( !$query->num_rows() )
            return false;

        foreach ( $query->result() as $row ) {
            $news = $row;
        }
        
        return $news;
    }
    
    function get_paged_list($limit = 20, $offset = 0, $order_column='datecreated', $order_type='DESC'){
        if(empty($order_column)||empty($order_type)){
            $this->db->order_by("datecreated",'DESC');	
    	}else{
            $this->db->order_by($order_column,$order_type);
            return $this->db->get($this->table, $limit, $offset);
    	}
    }
    function get_news_archived(){
        $this_year  = date('Y');
        $sql        = 'SELECT MONTH(datecreated) AS Month FROM '.$this->table.' WHERE datecreated LIKE "'.$this_year.'%" GROUP BY MONTH(datecreated)';
        $qry        = $this->db->query($sql);
        $month_list = array('Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'Nopember', 'Desember');
        $month_arr  = array();
        
        if($qry->num_rows > 0){
            $month  = $qry->result();
            foreach($month as $row){
                $month_arr[]    = $month_list[$row->Month - 1];
            }
            return $month_arr;
        }
        return false;
    }
    function count_all(){
        return $this->db->count_all($this->table);
    }
    function save($isi){
        if($this->db->insert($this->table,$isi)) return true;
        else return false;
    }
    function delete($id){
        if($this->db->delete($this->table, array($this->primary => $id))) return true;
        else return false; 
    }
    function update($id,$array){
	   $this->db->where($this->primary,$id);	
       if($this->db->update($this->table,$array)) return true;
       else return false;
    }
    function select_by_id($id,$mod="result"){
        $q = $this->db->query("SELECT * FROM `".$this->table."` WHERE `".$this->primary."` = '".$id."'");
        if(strtolower($mod) == "result") return $q->result();
        else return $q->num_rows();
    }
    
    function get_news_image($id){
        $sql    = '
            SELECT * FROM '.$this->table_link.' AS L 
            INNER JOIN '.$this->table_gallery.' AS G 
            ON L.id_gallery = G.id 
            WHERE L.id = '.$id.' AND L.type = "news" 
            ORDER BY G.datecreated DESC';
        $qry    = $this->db->query($sql);
        
        if($qry->num_rows > 0){
            return $qry->result();
        }
        return false;
    }
}
/* End of file model_news.php */
/* Location: ./application/models/model_news.php */
